/**
 * 
 */
package com.sq.partner.manager.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.GatewaySubChannel;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.TradCreditLmtDef;
import com.sq.partner.manager.mybatis.model.TradCreditLmtTrace;
import com.sq.partner.manager.service.ICreditLmtControlService;
import com.sq.partner.manager.service.IGatewayChannelInfoConfigService;
import com.sq.partner.manager.service.IGatewaySubChannelService;
import com.sq.partner.manager.util.DecimalUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

/**
 * @author Kang Zhiqiang
 * 额度控制
 *
 */
@Controller
@RequestMapping(value = "/creditLmtControl")
public class CreditLmtControlController{
	
	@Resource
	private ICreditLmtControlService creditLmtControlService;
	@Resource
	private IGatewayChannelInfoConfigService  channelInfoConfigService;
	@Resource
	private IGatewaySubChannelService subChannelServic;
	
	/**
	 * 进入creditLmtControl.jsp前跳转
	 * @return
	 */
	@RequestMapping("/creditLmtControl")
	public String creditLmtControl(){
		return "appManage/creditLmtControl";
	}
	
	/**
	 * 查询所有channelInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryChannelInfoList")
	@ResponseBody
	public List<GatewayChannelInfo> queryChannelInfoList() throws Exception{
		List<GatewayChannelInfo> channelInfoList = creditLmtControlService.queryChannelInfoListByChannelInfo(new GatewayChannelInfo());
		
		return channelInfoList;
	}
	
	/**
	 * 查询没有“creditLmtDef”的合作伙伴及其子机构
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryNotInCreditLmtDefChannelInfo")
	@ResponseBody
	public Page<Map> queryNotInCreditLmtDefChannelInfo(HttpSession session,Integer pageIndex,String subChannelCode,String subChannelName) throws Exception{
		GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
		String deptCode = user.getDepts().get(0).getDeptCode();
		
		Page<Map> page = new Page<Map>();
		
		//只有总公司或分公司机构才有操作权限
		if(StrUtil.isEmpty(deptCode) || deptCode.length() != 2){
			throw new AppException("操作权限不足！");
		}
		
		page.setCurrent(pageIndex);

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("subChannelCode", subChannelCode);
		map.put("subChannelName", subChannelName);
		map.put("deptCode", deptCode);
		
		page.setMap(map);
		
		Page<Map> channelInfoList = creditLmtControlService.queryNotInCreditLmtDefChannelInfo(page);
		
		return page;
	}
	
	/**
	 * 根据channelName分页查询TradCreditLmtDef及详细关联信息
	 * @param pageIndex
	 * @param channelName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryCreditLmtDefByChannelName")
	@ResponseBody
	public Page queryCreditLmtDefByChannelName(HttpSession session,Integer pageIndex,String channelName) throws Exception{
		GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
		
		Page creditLmtDefPage = creditLmtControlService.queryCreditLmtDefByChannelName(pageIndex, channelName,user);
		
		return creditLmtDefPage;
	}
	
	/**
	 * 添加或修改creditLmtDef
	 * @param alertLmt
	 * @param terminalLmt
	 * @param limitNo
	 * @param channelCode
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveOrUpdateCreditLmtDef")
	@ResponseBody
	public Result saveOrUpdateCreditLmtDef(BigDecimal alertLmt,BigDecimal terminalLmt,String limitNo,String channelCode,String selfOnly,HttpSession session) throws Exception{
		if(StrUtil.isEmpty(channelCode)){
			return Result.error("合作伙伴不能为空，请选择合作伙伴！");
		}
		GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
		String operator = user.getUserCode();
		GatewaySubChannel subChannel = subChannelServic.querySubChannelInfoById(channelCode);
		if(StrUtil.isNotEmpty(subChannel.getParentChannelCode())){//合作伙伴子机构
			selfOnly=null;
			TradCreditLmtDef creditLmtDef = new TradCreditLmtDef();
			creditLmtDef.setPartnerCode(subChannel.getParentChannelCode());
			creditLmtDef.setSelfOnly("N");
			creditLmtDef.setStatus("1");
			List<TradCreditLmtDef> list = creditLmtControlService.queryTradCreditLmtDefs(creditLmtDef);
			if(list!=null&&!list.isEmpty()){
				return Result.success("该子机构对应合作伙伴已有额度控制，不能添加子机构额度控制。");
			}
		}else{
			if("N".equals(selfOnly)){
				List<TradCreditLmtDef> list= creditLmtControlService.queryTradCreditLmtDefsByChannelCode(channelCode);
				if(list!=null&&!list.isEmpty()){
					return Result.success("该合作伙伴下子机构已有额度控制，不能添加合作伙伴额度控制。");
				}
			}
		}
		creditLmtControlService.saveOrUpdateCreditLmtDef(alertLmt, terminalLmt, limitNo, channelCode,selfOnly,operator);
		
		channelInfoConfigService.clearAllCacheByChannelCode(channelCode); //增加清理缓存的处理
		if(StrUtil.isNotEmpty(limitNo)){
			return Result.success("修改成功！");
		}else{
			return Result.success("添加成功！");
		}
	}
	
	/**
	 * 根据creditLmtDef属性删除相应creditLmtDef
	 * @param limitNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delCreditLmtDefByCreditLmtDef")
	@ResponseBody
	public Result delCreditLmtDefByCreditLmtDef(String limitNo,String channelCode) throws Exception{
		if(StrUtil.isEmpty(limitNo)){
			return Result.error("信用额度定义号不能为空");
		} else if(StrUtil.isEmpty(channelCode)){
			return Result.error("合作伙伴不能为空");
		}
		
		TradCreditLmtDef creditLmtDef = new TradCreditLmtDef();
		creditLmtDef.setLimitNo(limitNo);
		creditLmtControlService.delCreditLmtDefByCreditLmtDef(creditLmtDef);
		channelInfoConfigService.clearAllCacheByChannelCode(channelCode); //增加清理缓存的处理
		return Result.success("删除成功");
	}

	/**
	 * 保存添加保费
	 * @param limitNo
	 * @param partnerCode
	 * @param appendMoney
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveAppendMoney")
	@ResponseBody
	public Result saveAppendMoney(String limitNo,String partnerCode,BigDecimal appendMoney,String remarkDscp,HttpSession session) throws Exception{
	
		GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
		String operator = user.getUserCode();
		
		if(StrUtil.isEmpty(operator)){
			return Result.error("操作人不能为空");
		}else if(StrUtil.isEmpty(partnerCode)){
			return Result.error("合作伙伴代码不能为空");
		}else if(StrUtil.isEmpty(limitNo)){
			return Result.error("信用额度定义号不能为空");
		}else if(StrUtil.isEmpty(remarkDscp)){
			return Result.error("备注说明不能为空");
		}else if(remarkDscp.length()>160){
			return Result.error("备注说明最大录入160汉字");
		}
		
		DecimalUtil.toDouble(appendMoney, 2);
		
		TradCreditLmtTrace creditLmtTrace = new TradCreditLmtTrace();
		creditLmtTrace.setLimitNo(limitNo);
		creditLmtTrace.setPartnerCode(partnerCode);
		creditLmtTrace.setAppendMoney(appendMoney);
		creditLmtTrace.setOperator(operator);
		creditLmtTrace.setRemarkDscp(remarkDscp);
		creditLmtControlService.saveAppendMoney(creditLmtTrace);
	
		return Result.success("追加保费成功");
	}
	
	// 追加明细查询
	@RequestMapping("/queryAppendDetailInfoBylimtNo")
	@ResponseBody
	public Page queryCreditLmtDefByChannelName(Integer pageIndex,String limitNo,String beginTime,String endTime,BigDecimal appendBeginMoney,BigDecimal appendEndMoney) throws Exception{
		Page creditLmtDefPage = new Page();
		if(StrUtil.isEmpty(limitNo)){
			creditLmtDefPage.setResult("error");
			creditLmtDefPage.setMessage("信用额度代码不能为空");
			return creditLmtDefPage;
		}
		TradCreditLmtTrace creditLmtTrace = new TradCreditLmtTrace();
		creditLmtTrace.setLimitNo(limitNo);
		creditLmtTrace.setBeginTime(beginTime);
		creditLmtTrace.setEndTime(endTime);
		creditLmtTrace.setAppendBeginMoney(appendBeginMoney);
		creditLmtTrace.setAppendEndMoney(appendEndMoney);
		creditLmtDefPage = creditLmtControlService.queryAppendDetailInfoByLimitNo(pageIndex, creditLmtTrace);
		return creditLmtDefPage;
	}
}
